#===============================================
# Code written by Joe France (joeprogram.com)
# Released under code.google.com/p/video-scholar/
# with the MIT license (aka, the do-whatever-you-want-with-it license)
#===============================================


import pyglet
import Tkinter
import tkFileDialog

class State( object ):
	'''
	A State is a mode the program can be in
	And controls what elements are available
	'''
	
	def __init__( self, app ):
		
		self.app = app
		self.guis = []
		self.mouse_over_guis = []
		self.dialog_open = False
		
	def draw( self ):
		for gui in self.guis:
			gui.draw()
			
	def key_press( self, symbol, modifiers ):
		pass

	def mouse_motion( self, x, y, dx, dy ):
		'''
		The mouse has moved.
		See if its current position hittests with any guis
		or if it has left any guis that it used to intersect
		'''
		
		for gui in filter( lambda x: x not in self.mouse_over_guis, self.guis ):
			if gui.hit_test( x, y ):
				gui.mouse_over( x, y)
				self.mouse_over_guis.append( gui )
		
		for gui in self.mouse_over_guis:
			if not gui.hit_test( x, y ):
				gui.mouse_out( x, y)
				self.mouse_over_guis.remove( gui )
		
		
	def mouse_press( self, x, y, button, modifiers ):
		print self.__class__
		for gui in self.guis:
			if gui.hit_test( x, y ):
				gui.mouse_press( x, y, button, modifiers )

	def load_video( self ):	
		'''
		Load up a video in a new main state
		'''
				
		# Things get messy if we try to ask for an openfilename dialog if one already exists
		# So we use a lock to make sure there's only ever one.
		
		if not self.dialog_open:
			self.dialog_open = True
	
			# Tk tries to make a window before we use the file dialog
			# We don't want to use this extra window, so we hide it away
			root = Tkinter.Tk()
			root.withdraw()
	
			# NOTE: By providing the Video label several times, tk is able to group them together
			file_path = tkFileDialog.askopenfilename( filetypes = [
													("Video","*.avi"),
													("Video","*.wmv"),
													("Video","*.mp4"),
													("Video","*.mov"),
													("Timeline","*.xml"),
												  ]) 
			self.dialog_open = False								  
	
			if file_path:
				self.app.push_state( MainState( self.app, file_path) )				
			
		
# NOTE: We import this at the end to avoid circular dependency		
from MainState import MainState